package com.hex.ds.hdrs.period.task.util;

import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.period.task.po.PeriodTask;
import com.hex.ds.hdrs.period.task.po.TaskTrack;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class TaskTrackHelper {

    /**
     * 添加或更新任务路径
     *
     * @param task
     * @param taskTrack
     */
    public static void addTaskTrack(PeriodTask task, TaskTrack taskTrack) {
        if (task == null) {
            return;
        }
        Map<String, TaskTrack> taskTrackMap = task.getTaskTrackMap();
        taskTrackMap.put(taskTrack.getType(), taskTrack);
        log.debug("添加或更新任务轨迹，任务编码【{}】，业务日期：【{}】，操作【{}】", task.getTaskCode(), task.getBizDate(), taskTrack.getType());
    }

    /**
     * 任务加载成功
     *
     * @return
     */
    public static TaskTrack loadSucc(String type) {
        return new TaskTrack(type, true, "", HdrsDateUtil.getCurTs());
    }

    /**
     * 任务处理成功
     *
     * @return
     */
    public static TaskTrack processorSucc(String type) {
        return new TaskTrack(type, true, "", HdrsDateUtil.getCurTs());
    }

    /**
     * 任务处理异常
     *
     * @param error
     * @return
     */
    public static TaskTrack processorFail(String error) {
        return new TaskTrack("processor", false, error, HdrsDateUtil.getCurTs());
    }

    /**
     * 任务分发成功
     *
     * @return
     */
    public static TaskTrack dispatcherSucc(String type) {
        return new TaskTrack(type, true, "", HdrsDateUtil.getCurTs());
    }

    /**
     * 任务分发异常
     *
     * @param error
     * @return
     */
    public static TaskTrack dispatcherFail(String error) {
        return new TaskTrack("dispatcher", false, error, HdrsDateUtil.getCurTs());
    }

    /**
     * 任务执行成功
     *
     * @return
     */
    public static TaskTrack execSucc() {
        return new TaskTrack("exec", true, "", HdrsDateUtil.getCurTs());
    }

    /**
     * 任务执行失败
     *
     * @param error
     * @return
     */
    public static TaskTrack execFail(String error) {
        return new TaskTrack("exec", false, error, HdrsDateUtil.getCurTs());
    }

    /**
     * 所属标签下的作业上个业务日期还没全完成
     *
     * @param error
     * @return
     */
    public static TaskTrack labelFail(String error) {
        return new TaskTrack("processor", false, error, HdrsDateUtil.getCurTs());
    }
}
